home *** CD-ROM | disk | FTP | other *** search
- { FormulaBuilder }
- { YGB Software, Inc. }
- { Copyright 1995 Clayton Collie }
- { All rights reserved }
-
- {* Simple demonstration of the different variable access methods of *}
- {* a TExpression *}
- {* Also demonstrates registering a new function with the engine *}
- {* *}
- unit Varxmpfm;
- interface
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs,
- FBCalc,FBComp,
- Buttons, StdCtrls;
-
- type
- TVarform = class(TForm)
- ResultMemo: TMemo;
- Button1: TButton;
- Button2: TButton;
- BitBtn1: TBitBtn;
- Label1: TLabel;
- Button3: TButton;
- Button4: TButton;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- procedure Button4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- SimpleExpression : TExpression;
- Procedure TryVariables;
- Procedure TryVarptr;
- Procedure TryVariableList;
- Procedure TryStringValues;
- Procedure UpdateCalc(const i : integer;const x, y : longint);
- end;
-
- var
- Varform: TVarform;
-
- implementation
-
- {$R *.DFM}
- var myFuncId : integer;
-
- {Custom callback proc
- SYNTAX : MYFUNC(X, Y)
- RETURNS : X + Y
- }
- Procedure MyProc( nParamcount : byte;
- const params : TActParamList;
- var retvalue : TVALUEREC;
- var nErrCode : Integer;
- ExprData : longint); export;
- var
- intval1 : longint;
- intVal2 : longint;
- begin
- intval1 := params[0].vInteger;
- intVal2 := params[1].vInteger;
- retvalue.vInteger := intval1 + intval2;
- nErrcode := EXPR_SUCCESS;
- end;
-
-
- Procedure TVarform.UpdateCalc(const i : integer;const x, y : longint);
- var tmp : string[50];
- begin
- { Evaluate the expression and place results in memo }
- tmp := IntToStr(i)+ '. X = '+IntToStr(x) +
- ', Y = '+IntToStr(y)+', Result = ' + SimpleExpression.AsString;
- ResultMemo.Lines.Add(tmp);
- end;
-
-
- { Use the Variables array property }
- Procedure TVarform.TryVariables;
- var i : integer;
- x,y : TValueRec;
- tmp : string;
-
- begin
- ResultMemo.Lines.Add('Method 1 : Property access via Variables Property');
- x.vtype := vtInteger; { this is necessary. Note that even }
- y.vtype := vtInteger; { if x and y were defined as int variables }
- { Integers and Floats are type compatible, so }
- { we could set the value of an integer variable to }
- { a float, and vice versa }
- with simpleExpression do
- begin
- for i := 1 to 10 do
- begin
- x.vInteger := (Random(10) + 1) * i;
- y.vInteger := (Random(5) + 1) * i;
- variables['X'] := x;
- variables['Y'] := y;
- UpdateCalc(i,x.vInteger,y.vInteger);
- end;
- end;
- end;
-
-
- { Access variables using the VariableList property }
- Procedure TVarform.TryVariableList;
- var i : integer;
- x,y : TVariable;
- tmp : string;
-
- begin
- ResultMemo.Lines.Add('Method 3 : Access via VariableList Property');
- with simpleExpression do
- begin
- x := variableList[0]; { we know x was the first to be added }
- y := variableList[1]; { and y was the second }
- { we had to read the values first so that the }
- { vtype field of the Value structure would be set }
-
- for i := 1 to 10 do
- begin
- x.value.vInteger := (Random(10) + 1) * i;
- y.value.vInteger := (Random(5) + 1) * i;
- variableList[0] := x;
- variableList[1] := y;
- UpdateCalc(i,x.value.vInteger,y.value.vInteger);
- end;
- end;
- end;
-
-
- { Access variables using the StringValues property }
- Procedure TVarform.TryStringValues;
- var i : integer;
- xstr, ystr : string[10];
-
- begin
- ResultMemo.Lines.Add('Method 4 : Access via StringValues Property');
- with simpleExpression do
- begin
- for i := 1 to 10 do
- begin
- xstr := intTostr( (Random(10) + 1) * i );
- ystr := inttostr( (Random(5) + 1) * i );
- StringValues['X'] := xstr;
- StringValues['Y'] := ystr;
- UpdateCalc(i,strToInt(xstr),strToInt(ystr));
- end;
- end;
- end;
-
-
-
- { Use this method when you need greater speed. This method allows }
- { direct access to the variable data maintained internally by formulaBuilder }
- { be very cautious not to alter string pointers obtained through GetVarPtr }
- { i will be looking at an easy way of allowing you to work with strings }
- { using this method. }
- Procedure TVarform.TryVarPtr;
- var i : integer;
- x,y : ^longint;
- vtype : byte;
- tmp : string;
- res : integer;
-
- begin
- ResultMemo.Lines.Add('Method 2 : Direct access to variable data ');
- ResultMemo.Lines.Add('Formula : '+SimpleExpression.Formula);
- with simpleExpression do
- begin
- GetVarPtr('X',vtype,pointer(x)); { obtain a pointer to the variable data }
- GetVarPtr('Y',vtype,pointer(y)); { vtype returns the type of variable }
- for i := 1 to 10 do
- begin
- x^ := (Random(10)+ 1) * i;
- y^ := (Random(25)+ 1) * i;
- UpdateCalc(i,x^,y^);
- end;
- end;
- end;
-
-
- procedure TVarform.FormCreate(Sender: TObject);
- var s: String;
- begin
- SimpleExpression := TExpression.Create(NIL);
- MyFuncID := FBRegisterFunction ('MyFunc',vtInteger,'ii',2,MyProc);
- MessageDlg ('MyFuncID is: ' + IntToStr(MyFuncID),mtinformation,[mbok],0);
- s := 'Myfunc(x,y)';
- with simpleExpression do
- begin
- { NOTE! variables must be added before the formula property is set to }
- { an expression involving them. Otherwise, we get an Invalid Identifier error}
-
- AddVariable ('X',vtInteger);
- AddVariable ('Y',vtInteger);
- Formula := S;
- Randomize;
- end; {with}
- end;
-
- procedure TVarform.FormDestroy(Sender: TObject);
- begin
- FBUnregisterFunction(myFuncId);
- SimpleExpression.Free;
- end;
-
- procedure TVarform.Button1Click(Sender: TObject);
- begin
- TryVariables;
- end;
-
- procedure TVarform.Button2Click(Sender: TObject);
- begin
- TryVarPtr;
- end;
-
- procedure TVarform.Button3Click(Sender: TObject);
- begin
- TryVariableList;
- end;
-
- procedure TVarform.Button4Click(Sender: TObject);
- begin
- TryStringValues;
- end;
-
- end.
-